﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SunshineAirlines
{
    public partial class Ticket_Statisticscs : Form
    {
        public Ticket_Statisticscs()
        {
            InitializeComponent();
        }

        private void chart1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = "select format(Schedule.Date,'yyyy-MM') as Month, count(distinct Schedule.ScheduleId) as FlightsAmount,count(FlightReservation.ReservationId) as TicketsAmount,sum(FlightReservation.Payment) as ticketsRevenue from Schedule, FlightReservation where Status='Confirmed'and format(Schedule.Date,'yyyy-MM')<='" + dateTimePicker2.Value.ToString("yyyy-MM").Trim() + "' and format(Schedule.Date,'yyyy-MM')>='" + dateTimePicker1.Value.ToString("yyyy-MM").Trim() + "'group by format(Schedule.Date, 'yyyy-MM')";
            string view = comboBox1.Text.Trim();
            chartupdate(DBHelper.ExecuteTable(sql),view);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select format(Schedule.Date,'yyyy-MM') as Month, count(distinct Schedule.ScheduleId) as FlightsAmount,count(FlightReservation.ReservationId) as TicketsAmount,sum(FlightReservation.Payment) as ticketsRevenue from Schedule, FlightReservation where Status='Confirmed'and format(Schedule.Date,'yyyy-MM')<='"+dateTimePicker2.Value.ToString("yyyy-MM").Trim()+ "' and format(Schedule.Date,'yyyy-MM')>='" + dateTimePicker1.Value.ToString("yyyy-MM").Trim() + "'group by format(Schedule.Date, 'yyyy-MM')";
            this.dataGridView1.DataSource = DBHelper.ExecuteTable(sql);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                chart1.Series[0].IsValueShownAsLabel = true;
            }
            else
            {
                chart1.Series[0].IsValueShownAsLabel = false;
            }
        }

        private void chartupdate(DataTable dt, string vt)
        {
            // 添加坐标点 多敲几遍
            List<string> X = new List<string>();
            List<object> Y = new List<object>();
            if (dt != null)
            {
                foreach (DataRow row in dt.Rows)
                {
                    X.Add(row["Month"].ToString());
                    Y.Add(row[vt].ToString());
                }
                chart1.Series[0].Points.DataBindXY(X, Y);
            }
        }

        private void Ticket_Statisticscs_Load(object sender, EventArgs e)
        {

        }
    }
}
